package com.bluelinelabs.conductor.demo.controllers;

import com.bluelinelabs.conductor.conductor.manager.ObjectManager;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.service.DisplayManager;
import ohos.bundle.AbilityInfo;

import com.bluelinelabs.conductor.conductor.RouterTransaction;
import com.bluelinelabs.conductor.conductor.changehandler.FadeChangeHandler;
import com.bluelinelabs.conductor.demo.ResourceTable;
import com.bluelinelabs.conductor.demo.changehandler.EmptyChangeHandler;
import com.bluelinelabs.conductor.demo.controllers.base.BaseController;
import com.bluelinelabs.conductor.demo.util.RootItemMessage;


/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class HomeController extends BaseController {
    private Image id_fab;

    public HomeController() {
        super(ResourceTable.Layout_controller_home);
    }


    @Override
    public void onViewCreated(Component component) {
        title = "Conductor Demos";
        ObjectManager.getInstance().setIdTitleAboutSetVisibility(Component.VISIBLE);
        if (id_fab != null) {
            id_fab.setVisibility(Component.VISIBLE);
        }
        ListContainer Id_recycler_view = (ListContainer) component.findComponentById(ResourceTable.Id_recycler_view);
        HomeProvider homeProvider = new HomeProvider(DemoModel.values());
        Id_recycler_view.setItemProvider(homeProvider);

        id_fab = (Image) component.findComponentById(ResourceTable.Id_fab);
        ObjectManager.getInstance().setHomeControllerImage(id_fab);
        ShapeElement shapeElement = new ShapeElement(getActivity(), ResourceTable.Graphic_circle);
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.BLUE.getValue()));
        id_fab.setBackground(shapeElement);

        id_fab.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(ohos.agp.components.Component component) {
                id_fab.setVisibility(Component.HIDE);
                ObjectManager.getInstance().callOnClickTitleAbout();
            }
        });

    }


    private class HomeProvider extends BaseItemProvider {

        private DemoModel[] items;

        public HomeProvider(DemoModel[] items) {
            this.items = items;
        }


        @Override
        public int getCount() {
            return items.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {

            Component cpt;
            if (convertComponent == null) {
                cpt = LayoutScatter.getInstance(componentContainer.getContext()).parse(ResourceTable.Layout_row_home, componentContainer, false);
            } else {
                cpt = convertComponent;
            }
            DemoModel item = items[position];

            Text itemText = ((Text) cpt.findComponentById(ResourceTable.Id_title));
            itemText.setText(item.title);

            //按钮
            ShapeElement shapeElement = new ShapeElement(cpt.getContext(), ResourceTable.Graphic_circle);
            shapeElement.setRgbColor(RgbColor.fromArgbInt(componentContainer.getContext().getColor(item.color)));
            Image itemImage = ((Image) cpt.findComponentById(ResourceTable.Id_dot));
            itemImage.getContentPosition();
            itemImage.setBackground(shapeElement);

            RootItemMessage rootItemMessage = new RootItemMessage();
            rootItemMessage.setImageColor(item.color);
            rootItemMessage.setText(item.title);
            rootItemMessage.setImageWidth(itemImage.getWidth());
            rootItemMessage.setImageHeight(itemImage.getHeight());
            rootItemMessage.setTextHeight(itemText.getHeight());
            rootItemMessage.setTextWidth(itemText.getWidth());
            rootItemMessage.setTextSize(itemText.getTextSize());

            cpt.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    rootItemMessage.setImageLocation(itemImage.getLocationOnScreen());
                    rootItemMessage.setTextLocation(component.findComponentById(ResourceTable.Id_title).getLocationOnScreen());
                    onModelRowClick(item, position, rootItemMessage);
                }
            });
            return cpt;
        }
    }


    private void onModelRowClick(DemoModel model, int position, RootItemMessage rootItemMessage) {
        System.out.println("wtf onModelRowClick model = " + model + " position = " + position);
        switch (model) {
            case NAVIGATION:
                getRouter().pushController(RouterTransaction.with(new NavigationDemoController(0, NavigationDemoController.DisplayUpMode.SHOW_FOR_CHILDREN_ONLY))
                        .pushChangeHandler(new FadeChangeHandler()).popChangeHandler(new FadeChangeHandler()).tag(NavigationDemoController.TAG_UP_TRANSACTION));
                break;
            case TRANSITIONS:
                getRouter().pushController(TransitionDemoController.getRouterTransaction(0, this));
                break;
            case TARGET_CONTROLLER:
                getRouter().pushController(
                        RouterTransaction.with(new TargetDisplayController())
                                .pushChangeHandler(new FadeChangeHandler())
                                .popChangeHandler(new FadeChangeHandler())
                );
                break;
            case VIEW_PAGER:
            case VIEW_PAGER_2:
                getRouter().pushController(
                        RouterTransaction.with(new ViewPagerController())
                                .pushChangeHandler(new FadeChangeHandler())
                                .popChangeHandler(new FadeChangeHandler())
                );
                break;
            case CHILD_CONTROLLERS:
                getRouter().pushController(
                        RouterTransaction.with(new ParentController())
                                .pushChangeHandler(new FadeChangeHandler())
                                .popChangeHandler(new FadeChangeHandler())
                );
                break;
            case DRAG_DISMISS:
                getRouter().pushController(
                        RouterTransaction.with(new DragDismissController())
                                .pushChangeHandler(new FadeChangeHandler(false))
                                .popChangeHandler(new FadeChangeHandler())
                );
                break;
            case MASTER_DETAIL:
                getRouter().pushController(
                        RouterTransaction.with(new MasterDetailListController())
                                .pushChangeHandler(new FadeChangeHandler())
                                .popChangeHandler(new FadeChangeHandler())
                );

                break;
            case EXTERNAL_MODULES:
                getRouter().pushController(
                        RouterTransaction.with(new ExternalModulesController())
                                .pushChangeHandler(new FadeChangeHandler())
                                .popChangeHandler(new FadeChangeHandler())
                );
                break;
            case MULTIPLE_CHILD_ROUTERS:
                getRouter().pushController(
                        RouterTransaction.with(
                                new MultipleChildRouterController())
                                .pushChangeHandler(new FadeChangeHandler())
                                .popChangeHandler(new FadeChangeHandler())
                );
                break;
            case SHARED_ELEMENT_TRANSITIONS:
                getRouter().pushController(
                        RouterTransaction.with(new CityGridController(rootItemMessage)).popChangeHandler(new EmptyChangeHandler(1000))
                );
                break;

        }

    }


    private enum DemoModel {
        NAVIGATION("Navigation Demos", ResourceTable.Color_red_300),
        TRANSITIONS("Transition Demos", ResourceTable.Color_blue_grey_300),
        SHARED_ELEMENT_TRANSITIONS("Shared Element Demos", ResourceTable.Color_purple_300),
        CHILD_CONTROLLERS("Child Controllers", ResourceTable.Color_orange_300),
        VIEW_PAGER("ViewPager", ResourceTable.Color_green_300),
        VIEW_PAGER_2("ViewPager2", ResourceTable.Color_pink_300),
        TARGET_CONTROLLER("Target Controller", ResourceTable.Color_deep_orange_300),
        MULTIPLE_CHILD_ROUTERS("Multiple Child Routers", ResourceTable.Color_grey_300),
        MASTER_DETAIL("Master Detail", ResourceTable.Color_lime_300),
        DRAG_DISMISS("Drag Dismiss", ResourceTable.Color_teal_300),
        EXTERNAL_MODULES("Bonus Modules", ResourceTable.Color_deep_purple_300);

        String title;
        int color;

        DemoModel(String title, int color) {
            this.title = title;
            this.color = color;
        }

    }


}